<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>inote 云记事</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../css/layout.css" />
    <link rel="stylesheet" type="text/css" href="../css/component/bar.css" />
    <link rel="stylesheet" type="text/css" href="../css/component/panel.css"/>
    <style>
    	html,body{
            width: 100%;
            height: 100%;
        }
        #wrap{
            height: 100%;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-flex-flow: column;
            width: 100%;
        }
        #header{
            position: fixed;
        }
        #main{
            -webkit-box-flex: 1; 
            -webkit-flex: 1;  
            padding-top: 48px;
        }
        .css-test{
            width: 50px;
            height: 50px;
            margin-top: 10px;
            background-color: #2196F3;
            border-radius: 100%;
        }
        .tabActive{
            background-color: #42A5F5;
        }
        .con{
            padding: 15px 8px;
            color: #212121;
        }
        .con-header{
            width: 100%;
            height: auto;
        }
        .ino-panel{
            margin-bottom: 12px;
        }
        .main{
            padding: 0px;
            height: auto;
            font-size: 16px;
            line-height: 24px;
        }
        .main-title,.main-list{
            /*padding: 15px;*/
            height: 54px;
        }
        .main-title{
            background-color: rgba(0, 0, 0, 0.12);
        }
        .main-list{
            position: relative;
        }
        .date{
            float: left;
            width: 90px;
            height: 54px;
            text-align: center;
        }
        .date > h4{
            font-weight: bold;
            font-size: 16px;
            margin: 0px;
            margin-top: 6px;
        }
        .date > span{
            font-size: 12px;
        }
        .detail{
            float: left;
            height: 54px;
            line-height: 54px;
            padding-left: 21px;
        }
        .more{
            position: absolute;
            top: 0px;
            right: 0px;
            padding: 15px;
        }
        .main-list{
            border-top: 1px solid rgba(0, 0, 0, 0.12);
        }
    </style>
</head>
<body>
    <div id="wrap">
        <div id="header" class="ino-bar home-bar">
            <div class="title">inote</div>
            <table class="ino-bar-content">
                <tr>
                    <td>
                        <div class="bar-tab bar-tab-back" tapmode="tabActive" onclick="api.closeWin();">
                            <img class="listTab" src="../image/png/webpage/arrow_back.png">
                        </div>                        
                    </td>
                </tr>
            </table>
        </div>
        <div id="main">
            <div class="con">
                <div class="con-main">
                    
                </div> 
            </div>
        </div>
    </div>
        
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript">
	apiready = function(){
        writePagename();
		var passDetail = api.pageParam.passdetail;
        
        getSingleData(passDetail);
        // 获取所有每天的任务标题
        // 首先排序
        writeTaskTitle(districtArray(dateGroup));
        for (var i = 0; i < schedule.length; i++) {
            writeListTask(schedule[i]);
        };
        // alert(dateGroup);
        // alert(dateGroup);
        // alert(passDetail);
	};
    // =====================================================
    // 添加标题
    function writePagename() {
        var pagename = api.pageParam.pagename;
        $api.dom('.title').innerHTML = pagename;
    };
    var schedule = new Array();
    var dateGroup = new Array();
    
    function getSingleData(datas) {
        for (var i = 0; i < datas.length; i++) {
            var sdate = datas[i][7].substr(0, 8);
            // alert(sdate);
            dateGroup.push(sdate);
            schedule.push(datas[i]);
        };
    };
    // 过滤数组的重复值
    function districtArray(array){ 
        var result = new Array(); 
        for(var i in array){ 
            if(result.indexOf(array[i]) == -1){ 
                result.push(array[i]); 
            } 
        } 
        return result; 
    };
    // 写入一天内时间的标题
    function writeTaskTitle(dateGroup) {
        for (var i = 0; i < dateGroup.length; i++) {
            var thisDate = dateGroup[i].slice(0,4)+'-'+dateGroup[i].slice(4,6);
            var thisDay = dateGroup[i].slice(6,8)+"日";
            
            var todayTaskTitle = '<div class="ino-panel main" id="task'+dateGroup[i]+'"><div class="main-title"><div class="date"><h4>'+thisDay+'</h4><span>'+thisDate+'</span></div><div class="detail"><strong id="sum'+dateGroup[i]+'"></strong>件计划任务</div></div></div>';
            $api.append($api.dom('.con-main'), todayTaskTitle);
        }
    };
    // 插入具体数据
    function writeListTask(datas) {
        var mainId = '#task'+datas[7].substr(0, 8);
        var listId = 'list'+datas[7].substr(0, 8);
        var dayMoment = timeBucket(datas[5]);
        var thisTitle = datas[3];
        var thisTime = timeExtent(datas[5], datas[6]);
        var status = 'chevron_right';
        if (datas[2] === '1') {
            var status = 'check';
        };
        var tistTask = '<div class="main-list" id="'+listId+'" onclick="openDetailPage('+datas[0]+')"><div class="date"><h4>'+dayMoment+'</h4><span>'+thisTime+'</span></div><div class="detail">'+thisTitle+'</div><div class="more"><img src="../image/png/'+status+'.png" width="24"></div></div>';
        $api.append($api.dom(mainId), tistTask);
        sumTask(listId);
    };
    // 计算当天的任务数量并且写入标题栏
    var checkSumTask;
    var allList = 0;
    function sumTask(listId) {
        
        if (listId === checkSumTask) {
            allList = allList+1;
            $api.dom('#sum'+listId.substr(4, 12)).innerHTML = allList;
            // alert(allList);
        }else{
            checkSumTask = listId;
            allList = 1;
            $api.dom('#sum'+listId.substr(4, 12)).innerHTML = allList;
            // alert(allList);
        }
    };
    // 计算时间差
    function timeExtent(s, e) {
        var st = s.substr(11, 5);
        var et = e.substr(11, 5);
        return st+'-'+et;
    };
    // 划分时间段
    function timeBucket(s) {
        var st = parseInt(s.substr(11, 2));
        if (st >= 6 && st <= 8) {
            return '早上';
        }else if (st > 8 && st <= 11){
            return '上午';
        }else if (st > 11 && st <= 14){
            return '中午';
        }else if (st > 14 && st <= 17){
            return '下午';
        }else{
            return '晚上';
        }
    };
    // 打开任务的详情页
    function openDetailPage(id) {
        api.openWin({
            name: 'add_schedule',
            url: 'add_schedule.html',
            pageParam: {
                name: 'detailPage',
                pagename: '任务详情',
                taskId: id,
                pass: '0'
            }
        });
        // alert(id);
    };
</script>
</html>